<?php
/**
 * Created by IntelliJ IDEA.
 * User: fang.cai.liang@aliyun.com
 * Date: 2016/12/26
 * Time: 20:46
 */

namespace app\index\controller;

use think\Db;
use think\Exception;


/**
 * Class TestController
 * @package app\xs\controller
 */
class WyController {

    private $splitPattern = '/----|\s+|,|\|/';
    
    private $batchNum = 5000;
    
    private $totalNum = 0;

    private $successNum = 0;

    private $skipNum = 0;

    /*
     * 
 CREATE TABLE `wy_user` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(25) NULL,
  `email` VARCHAR(35) NULL,
  `paswd` VARCHAR(30) NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC));

     */
    
    public function indirto()
    {
        $path = '/Users/fangcailiang/Downloads/win';
        $times = 0;
        $handle = opendir($path);
        if($handle){
            while(false !== ($content = readdir($handle))){
                if(('.' == $content) || ('..' == $content)){
                    continue;
                }
                $newPath = $path.DIRECTORY_SEPARATOR.$content;
                if(is_dir($newPath)){
                    
                }else{
                    $times ++;
                    if($times < 15){
                        echo PHP_EOL.'开始导入 '.$newPath.' 文件 !'.PHP_EOL;
                        $this->into($newPath);
                    }
                }
            }
        }
    }
    
    public function into($file){
        $fp = fopen($file, 'r');
        $contentaArr = [];
        $line = 0;
        while(!feof($fp)){
            $contentaArr[$line] = fgets($fp);
            $this->totalNum ++;
            $line ++;
            if(($line == $this->batchNum)){
                $line = 0;
                if(($this->totalNum > $this->skipNum)){
                    $dataList = $this->parse($contentaArr);
                    if(!empty($dataList)){
                        Db::name('wy_user')->insertAll($dataList);
                        $this->successNum = $this->successNum + count($dataList);
                        echo '成功插入了 '.count($dataList).' 条数据!'.PHP_EOL;
                    }else{
                        throw new Exception('文件格式不支持: '.$file);
                    }
                }
                unset($contentaArr);
            }
        }
        echo '总共 '.($this->totalNum).' 条数据, 跳过 '.($this->skipNum).' 条数据, 成功插入了 '.($this->successNum).' 条数据!'.PHP_EOL;
    }
    
    private function parse($contentaArr){
        $dataList = [];
        if(!empty($contentaArr)){
            foreach ($contentaArr as $row) {
                $data = preg_split($this->splitPattern, $row);
                if(count($data) == 2){
                    if(strpos($data[0], '@') > 0){  // email ---- paswd 格式
                        $db['username'] = '';
                        $db['email'] = $data[0];
                        $db['paswd'] = $data[1];
                    }else{ // username ---- paswd 格式
                        $db['username'] = $data[0];
                        $db['email'] = '';
                        $db['paswd'] = $data[1];
                    }
                    if($this->checkLength($db)){
                        $dataList[] = $db;
                    }
                }
                if(count($data) == 3){
                    if(strpos($row, '----') > 0){  // email ---- paswd ---- username 格式
                        $db['email'] = $data[0];
                        $db['paswd'] = $data[1];
                        $db['username'] = $data[2];
                    }else{ // username   email   paswd 格式
                        $db['username'] = $data[0];
                        $db['email'] = $data[1];
                        $db['paswd'] = $data[2];
                    }
                    if($this->checkLength($db)){
                        $dataList[] = $db;
                    }
                }
            }
        }
        return $dataList;
    }

    private function checkLength($data){
        if(mb_strlen($data['email']) > 35){
            return false;
        }
        if(mb_strlen($data['paswd']) > 30){
            return false;
        }
        if(mb_strlen($data['username']) > 25){
            return false;
        }
        return true;
    }
}